var GWT = (function(mui) {
	mui.init({
		swipeBack: true
	});
	mui.plusReady(function() {
		plus.screen.lockOrientation("portrait-primary");
	});
	var LoadingDialog = {
		instance: null,
		count: 0,
		defaultConfig: {},
		show: function(title, config) {
			this.count += 1;
			if (window.plus) {
				if (!this.instance) {
					this.instance = plus.nativeUI.showWaiting(title || '加载中...', mui.extend(this.defaultConfig, config));
				}
			}
			// window.plus && plus.nativeUI.showWaiting(title || '加载中...', mui.extend(this.defaultConfig, config));
		},
		hide: function() {
			this.count -= this.count === 0 ? 0 : 1;
			mui.later(function() {
				if (this.count === 0) {
					!!this.instance && this.instance.close();
					this.instance = null;
				}
			}.bind(this), 300)
			// setTimeout(function () {
			//     window.plus && plus.nativeUI.closeWaiting()
			// },300);
		},
		destroy: function() {
			!!this.instance && this.instance.close();
			this.instance = null;
			this.count = 0;
		}
	};
	var Utils = {
		getInfoByAQI: function(aqi) {
			var aqiScore = Number(aqi || 0);
			var aqiInfo = {
				colorRGBA: '',
				color16: '',
				level: '',
				toastMessage: ''
			};
			if (0 <= aqiScore && aqiScore <= 50) {
				aqiInfo.colorRGBA = 'rgba(79, 223, 153, .8)';
				aqiInfo.color16 = '#4fdf99';
				aqiInfo.level = '优';
				aqiInfo.toastMessage = "此时，空气质量令人满意，基本无空气污染，各类人群可正常活动。"
			} else if (aqiScore <= 100) {
				aqiInfo.colorRGBA = 'rgba(253,221,42, .8)';
				aqiInfo.color16 = '#fddd2a';
				aqiInfo.level = '良';
				aqiInfo.toastMessage = "此时空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响，建议极少数异常敏感人群应减少户外活动。"

			} else if (aqiScore <= 150) {
				aqiInfo.colorRGBA = 'rgba(243,167,81,.8)'
				aqiInfo.color16 = '#f3a751';
				aqiInfo.level = '轻度';
				aqiInfo.toastMessage = "此时，易感人群症状有轻度加剧，健康人群出现刺激症状。建议儿童、老年人及心脏病、呼吸系统疾病患者应减少长时间、高强度的户外锻炼。"

			} else if (aqiScore <= 200) {
				aqiInfo.colorRGBA = 'rgba(253,145,117,.8)'
				aqiInfo.color16 = '#fd9175';
				aqiInfo.level = '中度';
				aqiInfo.toastMessage = "此时，进一步加剧易感人群症状，可能对健康人群心脏、呼吸系统有影响，建议疾病患者避免长时间、高强度的户外锻练，一般人群适量减少户外运动。"

			} else if (aqiScore <= 300) {
				aqiInfo.colorRGBA = 'rgba(134,111,241,.8)'
				aqiInfo.color16 = '#866ff1';
				aqiInfo.level = '重度';
				aqiInfo.toastMessage = "此时，心脏病和肺病患者症状显著加剧，运动耐受力降低，健康人群普遍出现症状，建议儿童、老年人和心脏病、肺病患者应停留在室内，停止户外运动，一般人群减少户外运动。"

			} else if (aqiScore <= 500) {
				aqiInfo.colorRGBA = 'rgba(224,91,138,.8)'
				aqiInfo.color16 = '#e05b8a';
				aqiInfo.level = '严重';
				aqiInfo.toastMessage = "此时，健康人群运动耐受力降低，有明显强烈症状，提前出现某些疾病，建议儿童、老年人和病人应当留在室内，避免体力消耗，一般人群应避免户外活动。"

			} else {
				aqiInfo.colorRGBA = 'rgba(200,200,200,1)';
				aqiInfo.color16 = '#fff';
				aqiInfo.level = '-';
				aqiInfo.toastMessage = "-"
			}
			return aqiInfo;
		},
		getAlarmInfo: function(AlarmLevelID, AlarmSource) {
			var colorMap = {
				0: "#2190ec",
				1: "#e6231c",
				2: "#ee5b09",
				3: "#efa517",
				4: "#0044b1",
				5: "#642882"
			};
			var iconMap = {
				1: "iconfont icon-alarm-3",
				2: "iconfont icon-alarm-2",
				3: 'iconfont icon-alarm-1',
				4: "iconfont icon-alarm-4",
				5: "iconfont icon-changgui"
			};

			return {
				icon: iconMap[Number(AlarmSource)],
				color: colorMap[Number(AlarmLevelID)]
			}
		},
		getAllArea: function() {

			return JSON.parse(localStorage.getItem("areas") || [])
		},
		saveArea: function(areasJson) {
			localStorage.setItem("areas", areasJson)
		},
		getAreaDataById: function(id) {
			var areas = this.getAllArea();
			if (!!areas) {
				for (var i = 0; i < areas.length; i++) {
					var area = areas[i];
					if (area.AreaId === id) {
						return area.AreaData
					}
				}
			}
			return null
		},
		saveUserInfo: function(userInfoJson) {
			localStorage.setItem("userInfo", userInfoJson)
		},
		getUserInfo: function() {
			return JSON.parse(localStorage.getItem("userInfo") || {})
		},
		getAccountID: function() {
			var userInfo = this.getUserInfo();
			return userInfo ? userInfo.Account.AccountID : null
		},
		getUserID: function() {
			var userInfo = this.getUserInfo();
			return userInfo ? userInfo.User.UserID : null;
		},
		hasRole: function(roleCode) {
			var userInfo = this.getUserInfo();
			return userInfo ? userInfo.Roles.some(function(role) {
				return role.RoleCode === roleCode;
			}) : false
		},
		fromTimeStamp: function(timeStr) {
			return timeStr.replace(/[^\d.]/g, '')
		},
		saveBasicInfo: function(basicInfoJson) {
			localStorage.setItem("basicInfo", basicInfoJson)
		},
		getBasicInfo: function() {
			return JSON.parse(localStorage.getItem("basicInfo") || {})
		},
		getAppInfo: function() {
			if (window.plus) {
				return window.plus.runtime
			}
		},
		versionCmp: function(v1, v2) {
			var s2i = function(s) {
				return s.split('').reduce(function(a, c) {
					var code = c.charCodeAt(0);
					if (48 <= code && code < 58) {
						a.push(code - 48);
					}
					return a;
				}, []).reduce(function(a, c) {
					return 10 * a + c;
				}, 0);
			};
			var a = v1.split('.').map(function(s) {
				return s2i(s);
			});
			var b = v2.split('.').map(function(s) {
				return s2i(s);
			});
			var n = a.length < b.length ? a.length : b.length;
			for (var i = 0; i < n; i++) {
				if (a[i] < b[i]) {
					return -1;
				} else if (a[i] > b[i]) {
					return 1;
				}
			}
			if (a.length < b.length) return -1;
			if (a.length > b.length) return 1;
			var last1 = v1.charCodeAt(v1.length - 1) | 0x20,
				last2 = v2.charCodeAt(v2.length - 1) | 0x20;
			return last1 > last2 ? 1 : last1 < last2 ? -1 : 0;
		},
		clearCache: function() {
			window.confirm("确定清清除所有缓存数据？") && localStorage.clear();
		},
		Date: {
			currentHour: function() {
				return moment().format('YYYY-MM-DD HH:00:00')
			},
			lastNHour: function(n) {
				return moment().add(n, 'hour').format('YYYY-MM-DD HH:00:00')
			},
			lastHour: function() {
				return moment().add(-1, 'hour').format('YYYY-MM-DD HH:00:00')
			},
			today: function() {
				return moment().format("YYYY-MM-DD")
			},
			dateBeforeToday: function(days) {
				return moment().subtract(days, 'days').format("YYYY-MM-DD")
			},
			yesterday: function() {
				return this.dateBeforeToday(1)
			},
			firstDayOfWeek: function() {
				return moment().startOf('isoWeek').format("YYYY-MM-DD")
			},
			firstDayOfMonth: function() {
				return moment().startOf('month').format("YYYY-MM-DD")
			},
			firstDayOfSeason: function() {
				return moment().startOf('quarter').format("YYYY-MM-DD")
			},
			firstDayOfYear: function() {
				return moment().format("YYYY-01-01")
			}

		}
	};
	var Router = {
		//打开新页面
		navigateTo: function(url, id, styles, aniShow, duration, showedCB) {
			if (window.plus) {
				plus.webview.open(url, id, styles || {
					'popGesture': 'close'
				}, aniShow || 'slide-in-right', duration, showedCB);
			} else {
				// mui.toast("plus未初始化")
				location.href = url;
			}
		},
		//关闭当前页面，跳转至新页面
		redirectTo: function(url, id, styles, extras) {
			if (window.plus) {
				var parentPage = plus.webview.currentWebview();
				var newPage = plus.webview.create(url, id, styles || {
					'popGesture': 'close'
				}, extras);
				newPage.show('slide-in-right', 200, function() {
					plus.webview.close(parentPage, "none");
				},{});
			} else {
				// mui.toast("plus未初始化")
				location.replace(url)
			}
		},

		navigateBack: function(params) {
			var opener;
			if (window.plus) {
				opener = plus.webview.currentWebview().opener();
			}
			mui.fire(opener, "onResult", params);
			mui.back();
		},
		getParams: function(name, href) {
			var href = href || window.location.href,
				value = '';
			if (name) {
				var reg = new RegExp(name + '=([^&]*)', 'g');
				href.replace(reg, function($0, $1) {
					value = decodeURI($1);
				});
			} else {
				value = {};
				var reg = /\b(\w+)=([^\/&]*)/g;
				href.replace(reg, function($0, $1, $2) {
					value[$1] = decodeURI($2);
				});
			}
			return value;
		}

	};
	return {
		Utils: Utils,
		Router: Router,
		LoadingDialog: LoadingDialog
	}

})(mui);
